import os
import tensorflow as tf

if __name__ == '__main__':
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

    x = tf.random.normal([2, 5, 5, 3])
    print(x.shape)
    print(x)
    # 卷积核的格式：k,k, cin cout
    w = tf.random.normal([3, 5, 3, 4])
    print(w)
    # [[0, 0], [pad_top, pad_bottom],[pad_left, pad_right], [0, 0]]
    # out = tf.nn.conv2d(x, w, strides=1, padding=[[0, 0], [0, 0], [0, 0], [0, 0]])
    # print(out)

    # 周围进行 padding
    # out = tf.nn.conv2d(x, w, strides=1, padding="SAME")
    # #  shape=(2, 5, 5, 4)
    # print(out)

    # 周围不进行任何不 padding
    out = tf.nn.conv2d(x, w, strides=1, padding="VALID")
    # shape=(2, 3, 3, 4)
    print("out: ")
    print(out.shape)
    print(out)

    b = tf.ones([4])
    print(b.shape)
    # 利用了广播机制
    result = out + b
    print("result: ")
    print(result)

    # [[[-1.4342659 - 1.1668144 - 3.908395 - 0.29177123]]
    #
    #  [[1.9325103    1.2219414 - 19.05111 - 5.324943]]
    #
    #  [[-13.441574     2.3460438    5.3478723    5.040958]]]]


    # [[[-0.43426585 - 0.16681445 - 2.908395     0.70822877]]
    #
    #  [[2.9325104    2.2219415 - 18.05111 - 4.324943]]
    #
    #  [[-12.441574     3.3460438    6.3478723    6.040958]]]]

